<template>
  <div class="min-h-screen flex flex-col">
    <NavBar @toggle-theme="toggleTheme" :isDarkMode="isDarkMode" />
    <main class="flex-grow pt-[56px]">
      <!-- 添加顶部内边距，为固定导航栏腾出空间 -->
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
    <AppFooter />

    <!-- SEO 状态面板（仅开发环境） -->
    <SEOStatus />
  </div>
</template>

<script>
import { ref, onMounted, watch } from "vue";
import useConfig from "@/stores/config";

export default {
  setup() {
    const isDarkMode = ref(false);
    const config = useConfig();
    config.init();
    // 检查本地存储的主题偏好
    onMounted(() => {
      const savedTheme = localStorage.getItem("theme");
      if (
        savedTheme === "dark" ||
        (!savedTheme &&
          window.matchMedia("(prefers-color-scheme: dark)").matches)
      ) {
        isDarkMode.value = true;
        document.documentElement.classList.add("dark");
      } else {
        isDarkMode.value = false;
        document.documentElement.classList.remove("dark");
      }
    });

    // 监听主题变化并保存到本地存储
    watch(isDarkMode, (newValue) => {
      if (newValue) {
        document.documentElement.classList.add("dark");
        localStorage.setItem("theme", "dark");
      } else {
        document.documentElement.classList.remove("dark");
        localStorage.setItem("theme", "light");
      }
    });

    // 切换主题
    const toggleTheme = () => {
      isDarkMode.value = !isDarkMode.value;
    };

    return {
      isDarkMode,
      toggleTheme,
    };
  },
};
</script>

<style>
/* 页面过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
